var emId;
window.onload=function () {
    findByItem(1);

}
function findByItem(pageNo) {
    axios.get("/room/findByItem",{
        params: {
            pageNo:pageNo,
            roomName:$n("findName").value,
            roomState:$n("findState").value,
            into:$n("findOk").value,
            roomSex:$n("findSex").value
        }
    }).then(resp=>{
        var info = resp.data;
        var str="";
        info.list.forEach(n=>{
            str+= `<tr><td>${n.roomName}</td><td>${n.roomMaxNumber}</td><td>${n.roomNumbers}</td><td>${n.roomState}</td>
<td>${n.roomSex}生宿舍</td><td><input type="button" value="删除" style="margin-left:20px;height: 30px" onclick="del(${n.id})">
<input type="button" value="查看房间" style="margin-left:20px;height: 30px" onclick="findById(${n.id})">
<input type="button" value="查看设施" style="margin-left:20px;height: 30px" onclick="findFsId(${n.id})"></td></tr>`
        });
        $n("roomData").innerHTML=str;
        var custr ="";
        for (var i=1;i<=info.totalPage;i++){
            custr+=`<a href="javascript:findByItem(${i})" style="margin-left: 10px;color:black">${i}</a>`;
        }
        $n("stuCutDiv").innerHTML=custr;
    });
}

function del(id) {
 axios.get("/room/del",{
     params: {
         id
     }
 }).then(resp=>{

     var info = resp.data;

     if (info=="ok"){
         findByItem(1);
     }else  {
         $n("infoDiv").style.visibility="visible";
     }
 });
}

function findById(id) {

    $n("findDiv").style.visibility="visible";
 axios.get("/room/findById",{
     params:{
         id
     }
 }).then(resp=>{
     var info = resp.data;
     $n("findRoomName").innerHTML=info.roomName;
     $n("findBdMax").innerHTML = info.roomMaxNumber;
     $n("findByState").innerHTML=info.roomState;
     $n("findBd").innerHTML = info.roomNumbers;
     $n("findBySex").innerHTML=info.roomSex+"生宿舍";
      fullTable(info.studentList);
 })
}

/**
 * 填充学生表格
 * @param speakList
 */
function fullTable(studentList){
    var str="";
    studentList.forEach(n=>{
        str+=`<tr><td>${n.studentName}</td><td>${n.classBean.className}</td>
<td>${n.studentPhone}</td></tr>`;
    })
    $n("findData").innerHTML=str;
}
function add() {
    axios.get("/room/add",{
        params:{
            roomName:$n("addName").value,
            roomSex:$n("addSex").value,
            roomMaxNumber:$n("addBd").value
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            $n("addDiv").style.visibility="hidden";
            findByItem(1);
        }
    })
}
function findFsId(id) {
    emId=id;
axios.get("/facilities/findByRoomId",{
    params:{
        roomId:id
    }
}).then(resp=>{
    var info = resp.data;
    var str = "";
    info.forEach(n=>{
        str+=`<tr><td>${n.damage}</td><td>${n.damageDate}</td><td>${n.damageState}</td>`;
        if (n.damageState=="未解决"){
        str+=`<td><input type="button" value="处理设施" onclick="handle(${n.id},${n.roomBean.id})"></td></tr>`
        }
    })
    $n("fsData").innerHTML=str;
})
    $n("fsDiv").style.visibility="visible";
}

function handle(id,roomId) {
    axios.get("/facilities/updateState",{
        params:{
            id:id,
            roomId: roomId
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findFsId(roomId);
        }
    })
}

function addFs() {
    axios.get("/facilities/add",{
        params:{
            state:$n("fsContent").value,
            roomId:emId
        }
    }).then(resp=>{
        if (resp.data=="ok"){
            findFsId(emId);
            $n("fsContent").value="";
        }
    })
}
function findStu() {
    $n("findDiv").style.visibility="hidden"
}
function addDiv() {
    $n("addDiv").style.visibility="visible";
}
function addStu() {
    $n("addDiv").style.visibility="hidden"
}
function delInfo() {
    $n("infoDiv").style.visibility="hidden"
}
function fssDiv() {
    $n("fsDiv").style.visibility="hidden";
    $n("fsContent").value="";
    findByItem(1);
}
